/*******************************************************************************
 * Simple C-K Editor
 * 
 * Copyright (C) 2009 Ingi BROWN - Centre de Gestion Scientifique - Ecole des Mines ParisTech
 * Licensed under the GNU General Public License Version 3.
 * 
 * Simple C-K Editor includes source code and libraries from the JUNG Project (Java Universal Network/Graph Framework) 
 * released under the BSD license. Copyright (c) 2003-2004,  Regents of the University of California and the JUNG Project.
 * 
 * See the License.txt file for more details.
 ******************************************************************************/

package edu.ensmp.ckEditor.main;

import edu.ensmp.ckEditor.ckModel.CKModel.Concept;
import edu.ensmp.ckEditor.ckModel.CKModel.Knowledge;
import edu.ensmp.ckEditor.gui.GraphPanel;
import edu.ensmp.ckEditor.gui.MenuBar;
import edu.ensmp.ckEditor.util.UiConstants;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JSplitPane;
import javax.swing.JTextField;



/**
 * Main window of the C-K Editor.
 * 
 * Layout is based on :
 * <ul>
 * <li>A menu bar
 * <li>A text field on the top
 * <li>A Split Pane under that with a Graph panel in each pane, one for Concepts
 * (left), one for Knowledge (right).
 * </ul>
 */
public class EditorMainWindow implements FocusListener {

	/**
	 * The main editor window constructor
	 */
	public EditorMainWindow() {

		initFrame();
		setMainGUI();
		setMenuBar();
		centerAndShowWindow();
	}

	/*
	 * Getters and Setters for the private variables
	 */

	/** @return the editorFrame */
	public JFrame getEditorFrame() {
		return editorFrame;
	}

	/** @return the panelLeft */
	public GraphPanel<Concept, Integer> getConceptPanel() {
		return conceptPanel;
	}

	/** @return the panelRight */
	public GraphPanel<Knowledge, Integer> getKnowledgePanel() {
		return knowledgePanel;
	}

	/** @return the propositionTextField */
	public JTextField getPropositionTextField() {
		return propositionTextField;
	}

	/**
	 * @param propositionTextField
	 *            the propositionTextField to set
	 */
	public void setPropositionTextField(JTextField propositionTextField) {
		this.propositionTextField = propositionTextField;
	}

	/*
	 * PRIVATE
	 */

	// Components of the GUI
	private JFrame editorFrame;

	private GridBagConstraints constraints;

	private JSplitPane splitPane;

	private GraphPanel<Concept, Integer> conceptPanel;

	private GraphPanel<Knowledge, Integer> knowledgePanel;

	private JTextField propositionTextField;

	/**
	 * Set up the main window frame
	 */
	private void initFrame() {
		editorFrame = new JFrame();
		editorFrame.setTitle(UiConstants.MAIN_WINDOW_TITLE);
		editorFrame.setSize(new Dimension(UiConstants.WINDOW_WIDTH,
				UiConstants.WINDOW_HEIGHT));
		editorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set to
																	// exit when
																	// we close
																	// the frame
	}

	private void setMainGUI() {
		// Instantiate the two panels
		conceptPanel = new GraphPanel<Concept, Integer>();
		knowledgePanel = new GraphPanel<Knowledge, Integer>();

		// Set the labels for each panel
		conceptPanel.setLabelName("Concepts");
		knowledgePanel.setLabelName("Knowledge");

		// Set the backgrounds color for these two panels
		conceptPanel.setBackground(UiConstants.BACKGROUND_COLOR);
		knowledgePanel.setBackground(UiConstants.BACKGROUND_COLOR);

		// Add focus Listeners to change label color when "tabbing" through the
		// panels
		conceptPanel.getVisuViewer().addFocusListener(this);
		knowledgePanel.getVisuViewer().addFocusListener(this);

		// Create a split pane, with the two previous panels in it.
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, conceptPanel,
				knowledgePanel);

		// Set up and tune the split pane divider
		splitPane.setOneTouchExpandable(false);
		splitPane.setDividerLocation(UiConstants.WINDOW_WIDTH / 2);
		splitPane.setDividerSize(UiConstants.DIVIDER_SIZE);
		splitPane.setBackground(UiConstants.DIVIDER_COLOR);

		// Set up a GridBag Layout
		editorFrame.getContentPane().setLayout(new GridBagLayout());
		constraints = new GridBagConstraints();

		// Initialize and Add the TextField to the top of the window
		propositionTextField = new JTextField(
				UiConstants.DEFAULT_TEXTFIELD_STRING); // initialize
		constraints.gridy = 0; // set to the top (line 0)
		constraints.gridwidth = GridBagConstraints.REMAINDER; // one column only
		constraints.fill = GridBagConstraints.HORIZONTAL; // fill the whole
															// column width
		editorFrame.add(propositionTextField, constraints); // add

		propositionTextField.addFocusListener(this);

		// Add the splitPane under the TextField
		constraints.fill = GridBagConstraints.BOTH; // fill all the space left
		constraints.gridy = 1; // set under textField (line 1)
		constraints.weightx = 1;
		constraints.weighty = 1;
		editorFrame.add(splitPane, constraints); // add
	}

	private void setMenuBar() {
		// Add the menuBar to the window
		JMenuBar menuBar = new MenuBar();
		editorFrame.setJMenuBar(menuBar);
	}

	private void centerAndShowWindow() {
		// Center Window
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension window = editorFrame.getSize();
		// ensure that no parts of aWindow will be off-screen
		if (window.height > screen.height) {
			window.height = screen.height;
		}
		if (window.width > screen.width) {
			window.width = screen.width;
		}
		int xCoord = (screen.width / 2 - window.width / 2);
		int yCoord = (screen.height / 2 - window.height / 2);
		editorFrame.setLocation(xCoord, yCoord);

		// Show window
		editorFrame.setVisible(true);
	}

	public void focusGained(FocusEvent event) {
		if (conceptPanel.getVisuViewer().isFocusOwner()) {
			conceptPanel.setLabelColor(UiConstants.FOCUSED_LABEL_COLOR);
		}
		if (knowledgePanel.getVisuViewer().isFocusOwner()) {
			knowledgePanel.setLabelColor(UiConstants.FOCUSED_LABEL_COLOR);
		}

	}

	public void focusLost(FocusEvent event) {
		if (!conceptPanel.getVisuViewer().isFocusOwner()) {
			conceptPanel.setLabelColor(UiConstants.UNFOCUSED_LABEL_COLOR);
		}
		if (!knowledgePanel.getVisuViewer().isFocusOwner()) {
			knowledgePanel.setLabelColor(UiConstants.UNFOCUSED_LABEL_COLOR);
		}
	}
}
